package com.example.demo.config;

import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptorAdapter;


/**
 * Created by 豆腐干Sama on 2018/5/21.
 */
public class PresenceChannelInterceptor extends ChannelInterceptorAdapter {
    @Override
    public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
        StompHeaderAccessor sha = StompHeaderAccessor.wrap(message);
        // ignore non-STOMP messages like heartbeat messages
        if(sha.getCommand() == null) {
            return;
        }

        switch(sha.getCommand()) {
            case CONNECT:
                connect();
                break;
            case CONNECTED:
                break;
            case DISCONNECT:
                disconnect(sha);
                break;
            default:
                break;
        }
    }

    //连接成功
    private void connect(){
        System.out.println("链接成功");
    }

    //断开连接
    private void disconnect(StompHeaderAccessor sha){
        System.out.println("断开连接");
    }
}
